import pyecharts.options as opts
from pyecharts.charts import Line, Timeline
# from pyecharts.globals import ThemeType
import pandas as pd
import numpy as np

class LineChart_temp_timeline():
    
    def __init__(self,dataset, place=0):
        self.paint_data=dataset
        self.place=place #九寨沟——0；四姑娘山——1

        #主程序入口
        self.main()
    
    
    #主程序入口函数实现
    def main(self):
        
        # 调用获取数据函数
        self.getData()
        # 调用绘图函数
        self.paint()
        
        
    def getData(self):
            
        df=self.paint_data.loc[:,['date','最高温度','最低温度']] #提取日期和气温数据
        # df["最高温度"] = df["最高温度"].str.replace("℃", "")
        # df["最低温度"] = df["最低温度"].str.replace("℃", "")
        
        self.paint_data=df
        # self.paint_data=df.loc[df['date'].str.contains(self.year)] #筛选指定年份的数据 
        # print(self.paint_data)


    def paint(self):
        
        if self.place==0:
            place='九寨沟'
            years=range(2013, 2017) #设置年份范围
            fileName="4.1 九寨沟2013-2016年气温变化折线图（含时间轴）.html"
        else:
            place='小金'
            years=range(2016, 2020) #设置年份范围
            fileName="4.1 小金2016-2019年气温变化折线图（含时间轴）.html"
        
        tl = Timeline() #设置时间轴
        for i in years:
            data=self.paint_data.loc[self.paint_data['date'].str.contains(str(i))] #筛选指定年份的数据
            day_name_list = np.array(data.loc[:,'date']).tolist()
            high_temperature = np.array(data.loc[:,'最高温度']).tolist()
            low_temperature = np.array(data.loc[:,'最低温度']).tolist()     
        
            line=(
                # Line(init_opts=opts.InitOpts(theme=ThemeType.DARK, width="900px", height="600px"))
                Line(init_opts=opts.InitOpts(width="900px", height="600px"))
                .add_xaxis(xaxis_data=day_name_list)
                .add_yaxis(
                    series_name="最高气温",
                    y_axis=high_temperature,
                    markpoint_opts=opts.MarkPointOpts(
                        data=[
                            opts.MarkPointItem(type_="max", name="最大值"),
                            opts.MarkPointItem(type_="min", name="最小值"),
                        ]
                    ),
                    markline_opts=opts.MarkLineOpts(
                        data=[opts.MarkLineItem(type_="average", name="平均值")]
                    ),
                )
                .add_yaxis(
                    series_name="最低气温",
                    y_axis=low_temperature,
                    markpoint_opts=opts.MarkPointOpts(
                        data=[opts.MarkPointItem(value=-2, name="年最低", x=1, y=-1.5)]
                    ),
                    markline_opts=opts.MarkLineOpts(
                        data=[
                            opts.MarkLineItem(type_="average", name="平均值"),
                            opts.MarkLineItem(symbol="none", x="90%", y="max"),
                            opts.MarkLineItem(symbol="circle", type_="max", name="最高点"),
                        ]
                    ),
                )
                .set_global_opts(
                    title_opts=opts.TitleOpts(title=place+" {} 年气温变化".format(i)),
                    tooltip_opts=opts.TooltipOpts(trigger="axis"),
                    toolbox_opts=opts.ToolboxOpts(is_show=True),
                    xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
                )
            )
            tl.add(line, "{}年".format(i))
        tl.render('resultFile/'+fileName)
        
        print(fileName+"已生成！")
            
